package com.itheima.activiti.controller.activiti;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.activiti.auth.annotation.IgnoreToken;
import com.itheima.activiti.common.R;
import com.itheima.activiti.dto.activiti.*;
import com.itheima.activiti.dto.dto.HistoryDetailDTO;
import com.itheima.activiti.dto.system.UserDTO;
import com.itheima.activiti.service.activiti.AssignmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/task")
@Api(tags = "任务")
public class TaskController {

    @Autowired
    private AssignmentService assignmentService;

    @GetMapping("/queryToDoTasks")
    @ApiOperation("查询待办任务")
    public R<Page<TaskDTO>> queryToDoTasks(@RequestParam(required = false) String taskName,
                                           @RequestParam(required = false) String processDefinitionName,
                                           @RequestParam(required = false, defaultValue = "1")int page,
                                           @RequestParam(required = false, defaultValue = "10")int pageSize){
        log.info("task name: {}, processDefinitionName: {}, page:{}, pageSize{}", taskName, processDefinitionName,page, pageSize);
        Page<TaskDTO> result = assignmentService.queryToDoTasks(taskName, processDefinitionName, page, pageSize);

        return R.success(result);
    }

    @PutMapping("/complete")
    @ApiOperation("审批通过")
    public R complete(@RequestBody CompleteDTO completeDTO){
        log.info("id: {}, 原因：{}", completeDTO.getBusinessId(), completeDTO.getComment());
        assignmentService.complete(completeDTO);
        return R.success();
    }

    @GetMapping("/queryDoneTasks")
    @ApiOperation(value = "已办")
    public R<Page<ProcessActivityDTO>> queryDoneTasks(@RequestParam(required = false, defaultValue = "1") int page,
                                                      @RequestParam(required = false, defaultValue = "10") int pageSize) {

        return R.success(assignmentService.queryDoneTasks(page, pageSize));
    }

    @GetMapping("/getRejectUser")
    @ApiOperation(value = "查询驳回人")
    public R<List<UserDTO>> getRejectUser(String taskId) {
        return R.success(assignmentService.getRejectUser(taskId));
    }

    @PutMapping("/reject")
    @ApiOperation("审批驳回")
    public R reject(@RequestBody RejectDTO rejectDTO){
        log.info("id: {}, 原因：{}", rejectDTO.getBusinessId(), rejectDTO.getComment());
        assignmentService.reject(rejectDTO);
        return R.success();
    }

    @PutMapping("/recall")
    @ApiOperation(value = "撤回")
    public R recall(@RequestBody RecallDTO recallDTO) {
        assignmentService.recall(recallDTO.getTaskId(), recallDTO.getComment());
        return R.success(recallDTO.getTaskId(), "撤回任务成功");
    }

    @PutMapping("/terminate")
    @ApiOperation(value = "终止")
    public R terminate(@RequestBody TerminateDTO terminateDTO) {
        assignmentService.terminate(terminateDTO.getTaskId(), terminateDTO.getComment());
        return R.success(terminateDTO.getTaskId(), "终止任务成功");
    }
    @IgnoreToken
    @SneakyThrows
    @GetMapping("getSvg")
    @ApiOperation(value = "获取流程活动图",produces = "application/octet-stream")
    @ApiImplicitParam(name = "processInstanceId",value = "流程实例id",paramType = "query",dataType = "String")
    public void getProcessInstanceDiagram(String processInstanceId, HttpServletResponse response){
        InputStream inputStream=assignmentService.getProcessInstanceDiagram(processInstanceId);
        response.setHeader("Content-Type","image/svg+xml");
        response.setHeader("Cache-Control","no-store,no-cache");
        IOUtils.copy(inputStream,response.getOutputStream());
    }
    @GetMapping("/getTaskHandleDetailInfo")
    @ApiOperation(value = "查询处理历史")
    public R<List<HistoryDetailDTO>> getTaskHandleDetailInfo(String processInstanceId) {
        return R.success(assignmentService.historyTaskList(processInstanceId));
    }

}

